﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlatRedBall.Math.Geometry;
using Emptiness.Scripting;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;
using FlatRedBall;
using Emptiness.Utils;
using Emptiness.Screens;

namespace Emptiness
{
    public class Map
    {
        private List<Tower> m_towerList;
        public List<string> m_soundsEffects;
        // 1 for mono .wav files, 2 for stereo
        const int numberOfChannels = 2;

        public List<Tower> TowerList
        {
            get { return m_towerList; }
        }

        public Map()
        {
            m_towerList = new List<Tower>();
            m_soundsEffects = new List<string>();
        }


        public void AddTower(string towerSprite, int mass)
        {
            if (!GameScreen.stopAll)
            {
                var tower = new Tower(towerSprite, "Tower" + m_towerList.Count, mass);
                float minimumDistance = 20;
                float maximumDistance = 30;
                SpriteManager.Camera.PositionRandomlyInView(tower, minimumDistance, maximumDistance);
                tower.VisibleRepresentation.X = tower.X;
                tower.VisibleRepresentation.Y = tower.Y;
                tower.Z = 0;
                m_towerList.Add(tower);
                /*if (!VerifyCollision(tower))
                {
                    m_towerList.Add(tower);
                }*/
            }
        }

        public void VerifyTowerTimeToLive()
        {

            for(int i = 0; i < m_towerList.Count; i++)
            {
                var tower = m_towerList[i];
                if (!SpriteManager.Camera.IsSpriteInView(tower.VisibleRepresentation))
                {
                    m_towerList.RemoveAt(i);
                    i--;
                    tower.Destroy();
                }
            }
        }

        private bool VerifyCollision(Tower newTower)
        {
            if (newTower.Collision.CollideAgainstMove(GameScreen.ball.Collision, 0, 0))
            {
                newTower.Destroy();
                return true;
            }
            for (int i = 0; i < TowerList.Count; i++)
            {
                var tower = TowerList[i];
                if (newTower.Collision.CollideAgainstMove(tower.Collision, 0, 0))
                {
                    newTower.Destroy();
                    return true;
                }

            }
            return false;
        }

        public void RemoveTowerFromManagers(int i)
        {
            var tower = m_towerList[i];
            m_towerList.RemoveAt(i);
            tower.Destroy();
        }

        public void RemoveTowers()
        {
            foreach (var tower in m_towerList)
            {
                tower.Destroy();
            }
        }

        public void AddSound(string soundName)
        {
            m_soundsEffects.Add(soundName);
        }

        public void PlaySound(int index)
        {
            MusicUtil.GetMusicUtil().PlaySoundEffects(m_soundsEffects[index]);
        }

        public void RemoveSounds()
        {
            m_soundsEffects.Clear();
        }

        public void PlaySong(string songName)
        {
            
            MusicUtil.GetMusicUtil().Play(songName);
        }
    }
}
